package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.activity.login.PhoneSmsLoginActivity;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.adapter.ObShopListAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.GoodsListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.ObShopListPresenter;
import com.mz.ozy.mvp.view.ObShopListContract;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 欧花专享
 */
public class ObShopListActivity extends BaseActivity<ObShopListContract.View, ObShopListPresenter> implements ObShopListContract.View {

    @BindView(R.id.gridview)
    RecyclerView gridview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.bannerIv)
    ImageView bannerIv;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;
    private ObShopListAdapter adapter;
    private List<GoodsListBean.GoodsPageBean.PageItemsBean> mBeanList = new ArrayList<>();
    private int tag;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ob_shop_list);
        ButterKnife.bind(this);
        initTitle2();
        tvCenterTitle.setText("欧花专享");
        initView();
    }

    private void initView() {
        ll_top.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        titleBar.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        tvBack.setImageResource(R.mipmap.white_back);
        tvCenterTitle.setTextColor(getResources().getColor(R.color.white));

        gridview.setLayoutManager(new GridLayoutManager(this, 2));
        gridview.setNestedScrollingEnabled(false);
        adapter = new ObShopListAdapter(mBeanList);
        gridview.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //商品详情
                final GoodsListBean.GoodsPageBean.PageItemsBean item = (GoodsListBean.GoodsPageBean.PageItemsBean) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_item_view:
                    case R.id.iv_search_car: {
                        Intent intent = new Intent(ObShopListActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", item.getId() + "");
                        intent.putExtra("tag", Constants.OB_TYPE);

                        startActivity(intent);
                        break;
                    }
                  /*  case R.id.iv_search_car: {
                        Intent intent = new Intent(ObShopListActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", item.getId() + "");
                        //intent.putExtra("show", 1);
                        startActivity(intent);
                        break;
                    }*/
                }
            }
        });

        smartRefreshLayout.setEnableRefresh(false);
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tag = 1;
                page = page + 1;
                mPresenter.getList(ObShopListActivity.this, page + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                tag = 0;
                page = 1;
                mPresenter.getList(ObShopListActivity.this, page + "");
            }
        });
        mPresenter.getList(ObShopListActivity.this, page + "");


        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.e("scrollY", scrollY + "");

                if (scrollY > 50) {
                    ll_top.setBackgroundColor(getResources().getColor(R.color.c_142d2a));
                    titleBar.setBackgroundColor(getResources().getColor(R.color.c_142d2a));
                } else {
                    ll_top.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    titleBar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                }


            }
        });

    }

    @Override
    public void getListSuccess(GoodsListBean bean) {
        if (TextUtils.isEmpty(bean.getBanner().getImgPath())) {

        }
        if (bean.getBanner().getImgPath().contains(",")) {
            GlideUtils.loadRoundImg(this, bannerIv, bean.getBanner().getImgPath().split(",")[0], R.mipmap.default_main_banner, 0);

        } else {
            GlideUtils.loadRoundImg(this, bannerIv, bean.getBanner().getImgPath(), R.mipmap.default_main_banner, 0);

        }


        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            mBeanList.clear();
            if (bean.getP().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        mBeanList.addAll(bean.getP().getPageItems());
        adapter.notifyDataSetChanged();
        if (bean.getP().getPageItems().size() == 0 || bean.getP().getPageSize() * bean.getP().getPageNumber() >= bean.getP().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }

    }

    @OnClick(R.id.upgradeVipIv)
    public void onViewClicked() {
        //startActivity(new Intent(this, UpVipActivity.class));
        startActivity(new Intent(this, JoinMemberActivity.class));
    }
}